/*
by txdnet.cn tonydon v1.3
 */

package txdnet.db;
import java.sql.*;
import java.util.Properties;
import java.io.*;
import txdnet.util.*;
/**
 * @see 
  <pre>
  use -Dtxdnet.db.config.path Config db.ini path
  db.ini put in WEB-INF/classes
  eg:
* 
#dbcp or proxool or (if dbpool.currentpool = empty default is jndi , you must set jndi.refname value)jndi
dbpool.currentpool=proxool
# jndi
# jndi.refname=
# dbcp
dbcp.DriverName=com.mysql.jdbc.Driver
dbcp.ConnectUrl=jdbc:mysql://localhost:3306/xstore?autoReconnect=true&useUnicode=true&characterEncoding=UTF8
dbcp.DataBaseUser=root
dbcp.DataBaseKey=78289ba28bf
dbcp.MaxActive=18
dbcp.MaxWait=8000
dbcp.MaxIdle=5
dbcp.MinIdle=3
dbcp.TimeBetweenEvictionRunsMillis=120000
dbcp.MinEvictableIdleTimeMillis=150000
dbcp.NumTestsPerEvictionRun=2
dbcp.TestOnBorrow=true
dbcp.TestOnReturn=false
dbcp.TestWhileIdle=false

# proxool
jdbc-0.proxool.alias=db-1
jdbc-0.proxool.driver-url=jdbc:mysql://localhost:3306/xstore?autoReconnect=true&useUnicode=true&characterEncoding=UTF8
jdbc-0.proxool.driver-class=com.mysql.jdbc.Driver
jdbc-0.user=root
jdbc-0.password=78289ba28bf
jdbc-0.proxool.house-keeping-sleep-time=120000
jdbc-0.proxool.house-keeping-test-sql=select CURRENT_DATE
jdbc-0.proxool.maximum-connection-count=20
jdbc-0.proxool.minimum-connection-count=4
jdbc-0.proxool.maximum-connection-lifetime=600000
jdbc-0.proxool.simultaneous-build-throttle=10
jdbc-0.proxool.recently-started-threshold=60000
jdbc-0.proxool.overload-without-refusal-lifetime=60000
jdbc-0.proxool.maximum-active-time=300000
jdbc-0.proxool.verbose=true
jdbc-0.proxool.trace=false
jdbc-0.proxool.fatal-sql-exception=Fatal error
jdbc-0.proxool.prototype-count=2
 </pre>
 * @author txdnet
 */
public class PoolFactory {
private PoolFactory(){}
private static Properties dbprop = null;
private static String currentDbPool = init();
//数据库连接池定义
public final static String DBPOOL_PROXOOL="proxool";
public final static String DBPOOL_DBCP="dbcp";

public static Connection getConnection(){
       Connection  conn = null;
    try{
         if(currentDbPool==null){
                conn = JndiPool.getConnection();
          }else{
               if(currentDbPool.equals(PoolFactory.DBPOOL_DBCP)){
                 conn = DbcpPool.getConnection();
                }else if(currentDbPool.equals(PoolFactory.DBPOOL_PROXOOL)){
                 conn = ProxoolPool.getConnection();
                }
        }
    }catch(Exception e){
        Logger.error(PoolFactory.class, e.toString());
        conn = null;
    }
    return conn;
}

private static String init(){
        return PoolFactory.getDbProperties().getProperty("dbpool.currentpool", null);
}

public static synchronized Properties getDbProperties(){
    
    if(dbprop!=null){
        return dbprop ; 
    }
        InputStream is = null;
        dbprop = new Properties();
    try{
        is = PoolFactory.class.getResourceAsStream("/db.ini") ;        
        if(is==null){
        // 查看环境属性是否定义文件路径
        is = new BufferedInputStream( new FileInputStream( new File(System.getProperty("txdnet.db.config.path"), "db.ini")));
        }
        dbprop.load(is);
        is.close();
    }catch(Exception e){
            Logger.error(PoolFactory.class, e.toString());
            dbprop = null;
    }
    return dbprop;
}
}